import re

# 读取输入文件
with open('input.txt', 'r', encoding='utf-8') as f:
    data = f.read()

# 正则表达式用于提取ChannelName和TimeShiftURL
pattern = r'ChannelName="([^"]+)",.*?TimeShiftURL="([^"]+)"'

# 查找所有匹配的频道名称和URL
matches = re.findall(pattern, data)

# 写入输出文件
with open('output.m3u', 'w', encoding='utf-8') as f:
    # 为文件写入M3U头部
    f.write('#EXTM3U\n')
    
    # 遍历所有的频道数据
    for match in matches:
        channel_name = match[0]
        rtsp_url = match[1]

        # 截断RTSP URL，去掉?及后面的部分
        rtsp_url = rtsp_url.split('?')[0]

        # 写入频道信息和URL，格式按需求修改
        f.write(f'#EXTINF:-1 ,{channel_name}\n')
        f.write(f'{rtsp_url}\n')

print("M3U文件已生成：output.m3u")